$(document).ready(function () {

    chrome.runtime.onMessage.addListener(function (message) {
        if (message.event == 'internal.user.login.response') {
            if (message.data.response) {
                $('#login-result').removeClass('loading')
                    .removeClass('error')
                    .text('登录成功，' + message.data.user.nickName);
                setTimeout(function () {
                    window.location.href = 'app.html';
                }, 1000);
            } else {
                $('#login-result').removeClass('loading')
                    .addClass('error')
                    .text(message.data.message);
                $('#login-submit').removeAttr('disabled');
            }
        }
    });

    $('#login-form').on('submit', function (e) {
        $('#login-submit').attr('disabled', 'disabled');
        $('#login-result').addClass('loading').text('');
        e.preventDefault();
        var d = $(this).serializeArray(), formData = {};
        $.each(d, function (i, data) {
            formData[data.name] = data.value;
        });
        chrome.runtime.sendMessage({
            event: 'internal.user.login',
            data: formData
        });
    })

});